#include "bsp_can.h"

#include "bsp_dwt.h"
#include "bsp_log.h"
#include "main.h"
#include "memory.h"
#include "stdlib.h"

/* can instance ptrs storage, used for recv callback */
// 在CAN产生接收中断会遍历数组,选出hcan和rxid与发生中断的实例相同的那个,调用其回调函数
// @todo: 后续为每个CAN总线单独添加一个can_instance指针数组,提高回调查找的性能
static CANInstance *can_instance[CAN_MX_REGISTER_CNT] = {NULL};
static uint8_t idx;  // 全局CAN实例索引,每次有新的模块注册会自增

/* ----------------two static function called by CANRegister()-------------------- */

/**
 * @brief 添加过滤器以实现对特定id的报文的接收,会被CANRegister()调用
 *        给CAN添加过滤器后,BxCAN会根据接收到的报文的id进行消息过滤,符合规则的id会被填入FIFO触发中断
 *        对于FDCAN，设置使用特定ID模式过滤。
 *
 * @note f407的bxCAN有28个过滤器,这里将其配置为前14个过滤器给CAN1使用,后14个被CAN2使用
 *       初始化时,奇数id的模块会被分配到FIFO0,偶数id的模块会被分配到FIFO1
 *       注册到CAN1的模块使用过滤器0-13,CAN2使用过滤器14-27
 *       FDCAN的消息RAM是所有FDCAN外设共用的。
 *       H723系列FDCAN过滤器数量完全在CubeMX中自定义，因此先做一次检查，再添加即可。
 *
 * @attention 你不需要完全理解这个函数的作用,因为它主要是用于初始化,在开发过程中不需要关心底层的实现
 *            享受开发的乐趣吧!如果你真的想知道这个函数在干什么,请联系作者或自己查阅资料(请直接查阅官方的reference
 * manual) FDCAN的教程较少，但是添加FDCAN的人已经发了一篇CSDN讲解了，可以参考一下
 *
 * @param _instance can instance owned by specific module
 */
static void CANAddFilter(CANInstance *_instance) {
#ifdef STM32H723xx
  static uint8_t can1_filter_idx = 0, can2_filter_idx = 0, can3_filter_idx = 0;
  uint8_t *filter_idx_p;

  if (_instance->can_handle == &hfdcan1) {
    filter_idx_p = &can1_filter_idx;
  } else if (_instance->can_handle == &hfdcan2) {
    filter_idx_p = &can2_filter_idx;
  } else if (_instance->can_handle == &hfdcan3) {
    filter_idx_p = &can3_filter_idx;
  } else {
    while (1) {
      // 报错
    }
  }

  FDCAN_FilterTypeDef fdcan_filter_conf;
  fdcan_filter_conf.IdType = FDCAN_STANDARD_ID;
  fdcan_filter_conf.FilterIndex = (*filter_idx_p)++;
  fdcan_filter_conf.FilterType = FDCAN_FILTER_MASK;

  fdcan_filter_conf.FilterConfig =
      (_instance->tx_id & 1) ? FDCAN_FILTER_TO_RXFIFO0
                             : FDCAN_FILTER_TO_RXFIFO1;  // 奇数id的模块会被分配到FIFO0,偶数id的模块会被分配到FIFO1
  fdcan_filter_conf.FilterID1 = _instance->rx_id;
  fdcan_filter_conf.FilterID2 = 0x7FF;

  HAL_FDCAN_ConfigFilter(_instance->can_handle, &fdcan_filter_conf);

#elifdef STM32F407xx
  CAN_FilterTypeDef can_filter_conf;
  static uint8_t can1_filter_idx = 0, can2_filter_idx = 14;  // 0-13给can1用,14-27给can2用

  can_filter_conf.FilterMode =
      CAN_FILTERMODE_IDLIST;  // 使用id list模式,即只有将rxid添加到过滤器中才会接收到,其他报文会被过滤
  can_filter_conf.FilterScale = CAN_FILTERSCALE_16BIT;  // 使用16位id模式,即只有低16位有效
  can_filter_conf.FilterFIFOAssignment =
      (_instance->tx_id & 1) ? CAN_RX_FIFO0 : CAN_RX_FIFO1;  // 奇数id的模块会被分配到FIFO0,偶数id的模块会被分配到FIFO1
  can_filter_conf.SlaveStartFilterBank =
      14;  // 从第14个过滤器开始配置从机过滤器(在STM32的BxCAN控制器中CAN2是CAN1的从机)
  can_filter_conf.FilterIdLow = _instance->rx_id
                                << 5;  // 过滤器寄存器的低16位,因为使用STDID,所以只有低11位有效,高5位要填0
  can_filter_conf.FilterBank = _instance->can_handle == &hcan1
                                   ? (can1_filter_idx++)
                                   : (can2_filter_idx++);  // 根据can_handle判断是CAN1还是CAN2,然后自增
  can_filter_conf.FilterActivation = CAN_FILTER_ENABLE;    // 启用过滤器

  HAL_CAN_ConfigFilter(_instance->can_handle, &can_filter_conf);
#endif
}

/**
 * @brief 在第一个CAN实例初始化的时候会自动调用此函数,启动CAN服务
 *
 * @note 此函数会启动CAN1并开启中断
 *       FDCAN的情况下，我们采用FIFO接收方式（而不是buffer），FIFO和buffer还有queue接收方式请自行查阅H723手册
 *       FDCAN比bxCAN多了一个全局过滤器，这里配置为全部拒绝，只接受指定ID。
 *
 */
void CANServiceInit() {
#ifdef STM32H723xx
  // 启动FDCAN1
  HAL_FDCAN_Start(&hfdcan1);
  HAL_FDCAN_ActivateNotification(&hfdcan1, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0);
  HAL_FDCAN_ActivateNotification(&hfdcan1, FDCAN_IT_RX_FIFO1_NEW_MESSAGE, 0);

  // 启动FDCAN2
  HAL_FDCAN_Start(&hfdcan2);
  HAL_FDCAN_ActivateNotification(&hfdcan2, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0);
  HAL_FDCAN_ActivateNotification(&hfdcan2, FDCAN_IT_RX_FIFO1_NEW_MESSAGE, 0);

  // 启动FDCAN3 (如果存在)
  HAL_FDCAN_Start(&hfdcan3);
  HAL_FDCAN_ActivateNotification(&hfdcan3, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0);
  HAL_FDCAN_ActivateNotification(&hfdcan3, FDCAN_IT_RX_FIFO1_NEW_MESSAGE, 0);

#elifdef STM32F407xx
  HAL_CAN_Start(&hcan1);
  HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING);
  HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO1_MSG_PENDING);
  HAL_CAN_Start(&hcan2);
  HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO0_MSG_PENDING);
  HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO1_MSG_PENDING);
#endif
}

/* ----------------------- two extern callable function -----------------------*/

CANInstance *CANRegister(CAN_Init_Config_s *config) {
  if (!idx) {
    CANServiceInit();  // 第一次注册,先进行硬件初始化
    LOGINFO("[bsp_can] CAN Service Init");
  }
  if (idx >= CAN_MX_REGISTER_CNT)  // 超过最大实例数
  {
    while (1) {
      LOGERROR("[bsp_can] CAN instance exceeded MAX num, consider balance the load of CAN bus");
    }
  }
  for (size_t i = 0; i < idx; i++) {  // 重复注册 | id重复
    if (can_instance[i]->rx_id == config->rx_id && can_instance[i]->can_handle == config->can_handle) {
      while (1) {
        LOGERROR("[}bsp_can] CAN id crash ,tx [%d] or rx [%d] already registered", &config->tx_id, &config->rx_id);
      }
    }
  }

  CANInstance *instance = (CANInstance *)malloc(sizeof(CANInstance));  // 分配空间
  memset(instance, 0, sizeof(CANInstance));                            // 分配的空间未必是0,所以要先清空
                                                                       // 进行发送报文的配置
#ifdef STM32H723xx
  instance->txconf.Identifier = config->tx_id;                   // 发送id
  instance->txconf.IdType = FDCAN_STANDARD_ID;                   // 使用标准id,扩展id则使用CAN_ID_EXT(目前没有需求)
  instance->txconf.TxFrameType = FDCAN_DATA_FRAME,               // 发送数据帧
      instance->txconf.DataLength = FDCAN_DLC_BYTES_8,           // 数据长度为8字节
      instance->txconf.ErrorStateIndicator = FDCAN_ESI_ACTIVE,   // 兼容CAN2.0,错误状态指示器设为主动
      instance->txconf.BitRateSwitch = FDCAN_BRS_OFF,            // 兼容CAN2.0禁用位速率切换
      instance->txconf.FDFormat = FDCAN_CLASSIC_CAN,             // 使用经典CAN格式
      instance->txconf.TxEventFifoControl = FDCAN_NO_TX_EVENTS,  // 不需要，禁用事件FIFO
      instance->txconf.MessageMarker = 0;                        // 不使用消息标记
#elifdef STM32F407xx
  instance->txconf.StdId = config->tx_id;  // 发送id
  instance->txconf.IDE = CAN_ID_STD;       // 使用标准id,扩展id则使用CAN_ID_EXT(目前没有需求)
  instance->txconf.RTR = CAN_RTR_DATA;     // 发送数据帧
  instance->txconf.DLC = 0x08;             // 默认发送长度为8
#endif
  // 设置回调函数和接收发送id
  instance->can_handle = config->can_handle;
  instance->tx_id = config->tx_id;  // 好像没用,可以删掉
  instance->rx_id = config->rx_id;
  instance->can_module_callback = config->can_module_callback;
  instance->id = config->id;

  CANAddFilter(instance);          // 添加CAN过滤器规则
  can_instance[idx++] = instance;  // 将实例保存到can_instance中

  return instance;  // 返回can实例指针
}

/* @todo 目前似乎封装过度,应该添加一个指向tx_buff的指针,tx_buff不应该由CAN instance保存 */
/* 如果让CANinstance保存txbuff,会增加一次复制的开销 */
uint8_t CANTransmit(CANInstance *_instance, float timeout) {
  static uint32_t busy_count;
  static volatile float wait_time __attribute__((unused));  // for cancel warning
  float dwt_start = DWT_GetTimeline_ms();
#ifdef STM32H723xx
  while (HAL_FDCAN_GetTxFifoFreeLevel(_instance->can_handle) == 0)
#elifdef STM32F407xx
  while (HAL_CAN_GetTxMailboxesFreeLevel(_instance->can_handle) == 0)  // 等待邮箱空闲
#endif
  {
    if (DWT_GetTimeline_ms() - dwt_start > timeout)  // 超时
    {
      LOGWARNING("[bsp_can] CAN MAILbox full! failed to add msg to mailbox. Cnt [%d]", busy_count);
      busy_count++;
      return 0;
    }
  }
  wait_time = DWT_GetTimeline_ms() - dwt_start;

#ifdef STM32H723xx
  if (HAL_FDCAN_AddMessageToTxFifoQ(_instance->can_handle, &_instance->txconf, _instance->tx_buff) != HAL_OK)
#elifdef STM32F407xx
  // tx_mailbox会保存实际填入了这一帧消息的邮箱,但是知道是哪个邮箱发的似乎也没啥用
  if (HAL_CAN_AddTxMessage(_instance->can_handle, &_instance->txconf, _instance->tx_buff, &_instance->tx_mailbox))
#endif
  {
    LOGWARNING("[bsp_can] CAN bus BUSY! cnt:%d", busy_count);
    busy_count++;
    return 0;
  }
  return 1;  // 发送成功
}

void CANSetDLC(CANInstance *_instance, uint8_t length) {
  // 发送长度错误!检查调用参数是否出错,或出现野指针/越界访问
#ifdef STM32F407xx
  if (length > 8 || length == 0)  // 安全检查
    while (1) {
      LOGERROR("[bsp_can] CAN DLC error! check your code or wild pointer");
    }
  if (length > 8 || length == 0)  // 安全检查
    while (1) {
      LOGERROR("[bsp_can] CAN DLC error! check your code or wild pointer");
    }
  _instance->txconf.DLC = length;
#elifdef STM32H723xx
  // 根据长度设置对应的DLC值
  uint32_t dlc_value;

  if (length <= 8) {
    // 经典CAN模式下的DLC映射
    switch (length) {
      case 0:
        dlc_value = FDCAN_DLC_BYTES_0;
        break;
      case 1:
        dlc_value = FDCAN_DLC_BYTES_1;
        break;
      case 2:
        dlc_value = FDCAN_DLC_BYTES_2;
        break;
      case 3:
        dlc_value = FDCAN_DLC_BYTES_3;
        break;
      case 4:
        dlc_value = FDCAN_DLC_BYTES_4;
        break;
      case 5:
        dlc_value = FDCAN_DLC_BYTES_5;
        break;
      case 6:
        dlc_value = FDCAN_DLC_BYTES_6;
        break;
      case 7:
        dlc_value = FDCAN_DLC_BYTES_7;
        break;
      case 8:
        dlc_value = FDCAN_DLC_BYTES_8;
        break;
      default:
        while (1) LOGERROR("[bsp_fdcan] FDCAN DLC error! Invalid length for classic CAN");
    }
  } else if (length <= 64) {
    // CAN FD模式下的DLC映射
    if (length <= 12)
      dlc_value = FDCAN_DLC_BYTES_12;
    else if (length <= 16)
      dlc_value = FDCAN_DLC_BYTES_16;
    else if (length <= 20)
      dlc_value = FDCAN_DLC_BYTES_20;
    else if (length <= 24)
      dlc_value = FDCAN_DLC_BYTES_24;
    else if (length <= 32)
      dlc_value = FDCAN_DLC_BYTES_32;
    else if (length <= 48)
      dlc_value = FDCAN_DLC_BYTES_48;
    else
      dlc_value = FDCAN_DLC_BYTES_64;

    // 需要切换到CAN FD模式
    _instance->txconf.FDFormat = FDCAN_FD_CAN;
    _instance->txconf.BitRateSwitch = FDCAN_BRS_ON;
  } else {
    while (1) LOGERROR("[bsp_fdcan] FDCAN DLC error! Maximum length is 64 bytes");
  }

  _instance->txconf.DataLength = dlc_value;

#endif
}

/* -----------------------belows are callback definitions--------------------------*/

// 对于FDCAN，回调函数和处理方式完全不同，因此直接用两套逻辑处理
#ifdef STM32H723xx
/**
 * @brief 此函数会被下面两个函数调用,用于处理FIFO0和FIFO1溢出中断(说明收到了新的数据)
 *        所有的实例都会被遍历,找到can_handle和rx_id相等的实例时,调用该实例的回调函数
 *
 * @param _fdhcan
 * @param fifox passed to HAL_CAN_GetRxMessage() to get mesg from a specific fifo
 */
static void FDCANFIFOxCallback(FDCAN_HandleTypeDef *_hfdcan, uint32_t fifox) {
  FDCAN_RxHeaderTypeDef rxconf;
  uint8_t fdcan_rx_buff[64];
  while (HAL_FDCAN_GetRxFifoFillLevel(_hfdcan, fifox))  // FIFO不为空
  {
    if (HAL_FDCAN_GetRxMessage(_hfdcan, fifox, &rxconf, fdcan_rx_buff) == HAL_OK) {
      for (size_t i = 0; i < idx; ++i) {
        // 找到对应的实例
        if (_hfdcan == can_instance[i]->can_handle && rxconf.Identifier == can_instance[i]->rx_id) {
          // 回调函数不为空就调用
          if (can_instance[i]->can_module_callback != NULL) {
            // 根据DLC值计算实际数据长度
            uint8_t rx_length = 0;
            switch (rxconf.DataLength) {
              case FDCAN_DLC_BYTES_0:
                rx_length = 0;
                break;
              case FDCAN_DLC_BYTES_1:
                rx_length = 1;
                break;
              case FDCAN_DLC_BYTES_2:
                rx_length = 2;
                break;
              case FDCAN_DLC_BYTES_3:
                rx_length = 3;
                break;
              case FDCAN_DLC_BYTES_4:
                rx_length = 4;
                break;
              case FDCAN_DLC_BYTES_5:
                rx_length = 5;
                break;
              case FDCAN_DLC_BYTES_6:
                rx_length = 6;
                break;
              case FDCAN_DLC_BYTES_7:
                rx_length = 7;
                break;
              case FDCAN_DLC_BYTES_8:
                rx_length = 8;
                break;
              case FDCAN_DLC_BYTES_12:
                rx_length = 12;
                break;
              case FDCAN_DLC_BYTES_16:
                rx_length = 16;
                break;
              case FDCAN_DLC_BYTES_20:
                rx_length = 20;
                break;
              case FDCAN_DLC_BYTES_24:
                rx_length = 24;
                break;
              case FDCAN_DLC_BYTES_32:
                rx_length = 32;
                break;
              case FDCAN_DLC_BYTES_48:
                rx_length = 48;
                break;
              case FDCAN_DLC_BYTES_64:
                rx_length = 64;
                break;
              default:
                rx_length = 8;
                break;
            }
            can_instance[i]->rx_len = rx_length;                         // 保存接收到的数据长度
            memcpy(can_instance[i]->rx_buff, fdcan_rx_buff, rx_length);  // 消息拷贝到对应实例
            can_instance[i]->can_module_callback(can_instance[i]);       // 触发回调进行数据解析和处理
          }
          break;
        }
      }
    }
  }
}

void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo0ITs) {
  if ((RxFifo0ITs & FDCAN_IT_RX_FIFO0_NEW_MESSAGE) != 0) {
    FDCANFIFOxCallback(hfdcan, FDCAN_RX_FIFO0);
  }
}
void HAL_FDCAN_RxFifo1Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo1ITs) {
  if ((RxFifo1ITs & FDCAN_IT_RX_FIFO1_NEW_MESSAGE) != 0) {
    FDCANFIFOxCallback(hfdcan, FDCAN_RX_FIFO1);
  }
}

#elifdef STM32F407xx

/**
 * @brief 此函数会被下面两个函数调用,用于处理FIFO0和FIFO1溢出中断(说明收到了新的数据)
 *        所有的实例都会被遍历,找到can_handle和rx_id相等的实例时,调用该实例的回调函数
 *
 * @param _hcan
 * @param fifox passed to HAL_CAN_GetRxMessage() to get mesg from a specific fifo
 */
static void CANFIFOxCallback(CAN_HandleTypeDef *_hcan, uint32_t fifox) {
  static CAN_RxHeaderTypeDef rxconf;  // 同上
  uint8_t can_rx_buff[8];
  while (HAL_CAN_GetRxFifoFillLevel(_hcan, fifox))  // FIFO不为空,有可能在其他中断时有多帧数据进入
  {
    HAL_CAN_GetRxMessage(_hcan, fifox, &rxconf, can_rx_buff);  // 从FIFO中获取数据
    for (size_t i = 0; i < idx; ++i) {                         // 两者相等说明这是要找的实例
      if (_hcan == can_instance[i]->can_handle && rxconf.StdId == can_instance[i]->rx_id) {
        if (can_instance[i]->can_module_callback != NULL)  // 回调函数不为空就调用
        {
          can_instance[i]->rx_len = rxconf.DLC;                       // 保存接收到的数据长度
          memcpy(can_instance[i]->rx_buff, can_rx_buff, rxconf.DLC);  // 消息拷贝到对应实例
          can_instance[i]->can_module_callback(can_instance[i]);      // 触发回调进行数据解析和处理
        }
        return;
      }
    }
  }
}

/**
 * @brief 注意,STM32的两个CAN设备共享两个FIFO
 * 下面两个函数是HAL库中的回调函数,他们被HAL声明为__weak,这里对他们进行重载(重写)
 * 当FIFO0或FIFO1溢出时会调用这两个函数
 */
// 下面的函数会调用CANFIFOxCallback()来进一步处理来自特定CAN设备的消息

/**
 * @brief rx fifo callback. Once FIFO_0 is full,this func would be called
 *
 * @param hcan CAN handle indicate which device the oddest mesg in FIFO_0 comes from
 */
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan) {
  CANFIFOxCallback(hcan, CAN_RX_FIFO0);  // 调用我们自己写的函数来处理消息
}

/**
 * @brief rx fifo callback. Once FIFO_1 is full,this func would be called
 *
 * @param hcan CAN handle indicate which device the oddest mesg in FIFO_1 comes from
 */
void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan) {
  CANFIFOxCallback(hcan, CAN_RX_FIFO1);  // 调用我们自己写的函数来处理消息
}

#endif
